//
//  PKGroupListViewController.m
//  PiankeProject
//
//  Created by lanou on 15/10/10.
//  Copyright (c) 2015年 xiaolizi. All rights reserved.
//

#import "PKGroupListViewController.h"
#import "PKGroupListModel.h"
#import "PKGroupLatestpostsModel.h"
#import "PKRequestManager.h"
#import "PKGroupListCell.h"
#import "PKGroupListDetailViewController.h"
@interface PKGroupListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) NSMutableArray *latestArr;
@property(nonatomic,strong) NSMutableArray *listArray;
@property(nonatomic,strong) UITableView *mainTableView;
@property(nonatomic,strong) UISegmentedControl *segmentedControl;
@property(nonatomic,strong) NSMutableDictionary *parDic;
@end

@implementation PKGroupListViewController

- (NSMutableArray *)latestArr
{
    if (!_latestArr)
    {
        self.latestArr = [NSMutableArray array];
    }
    return _latestArr;
}

- (NSMutableArray *)listArray
{
    if (!_listArray)
    {
        self.listArray = [NSMutableArray array];
    }
    return _listArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestData];
    [self configureView];
    // 注册cell
    [_mainTableView registerClass:[PKGroupListCell class] forCellReuseIdentifier:@"list"];
}

- (void)configureView
{
    _segmentedControl =[[UISegmentedControl alloc] initWithItems:@[@"Hot",@"New"]];
    _segmentedControl.frame =  CGRectMake(SCREEN_WIDTH / 2 - 60, 10, 100, 20);
    _segmentedControl.tintColor = [UIColor blackColor];
    _segmentedControl.selectedSegmentIndex = 0;
    [_segmentedControl addTarget:self action:@selector(cilckSementedControl:) forControlEvents:(UIControlEventValueChanged)];
    [self.view  addSubview:_segmentedControl];
    
    self.mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT -110) style:UITableViewStylePlain];
    self.mainTableView.dataSource = self;
    self.mainTableView.delegate = self;
    self.mainTableView.rowHeight = 210;
    self.mainTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_mainTableView];

}

- (void)setMyParDic
{
    _parDic = [NSMutableDictionary dictionary];
    _parDic[@"devideid"] = @"63A94D37-33F9-40FF-9EBB-481182338873";
    _parDic[@"client"] = @"1";
    _parDic[@"sort"] = @"hot";
    _parDic[@"limit"] = @"10";
    _parDic[@"version"] = @"3.0.2";
    _parDic[@"start"] = @"0";
    _parDic[@"auth"] = @"Wc06FCrkoq1DCMVzGMTikDJxQ8bm3Mrm2NpT9qWjwzcWP23tBKQx1c4P0";
}
// 请求数据
- (void)requestData
{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self setMyParDic];
    });
    [PKRequestManager requestWithType:POST url:@"http://api2.pianke.me/group/group_list" parDic:self.parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSArray *arr = dic[@"data"][@"list"];
        self.listArray = nil;
        for (NSDictionary *listDic in arr)
        {
            PKGroupListModel *listModel = [[PKGroupListModel alloc] init];
            [listModel setValuesForKeysWithDictionary:listDic];
            [self.listArray addObject:listModel];
        }
        [_mainTableView reloadData];
    } error:nil];
}

- (void)cilckSementedControl:(UISegmentedControl *)segControl
{
    [self setMyParDic];
    switch (segControl.selectedSegmentIndex)
    {
        case 0:
            _parDic[@"sort"] = @"hot";
            [_mainTableView reloadData];
            break;
        case 1:
            _parDic[@"sort"] = @"addtime";
            [_mainTableView reloadData];
            break;
        default:
            break;
    }
    [self requestData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKGroupListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"list" forIndexPath:indexPath];
    cell.listModel = _listArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKGroupListDetailViewController *detailVC = [[PKGroupListDetailViewController alloc] init];
    detailVC.listModel = _listArray[indexPath.row];
    [self.navi.navigationController pushViewController:detailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
